home *** CD-ROM | disk | FTP | other *** search
/ Hardcore Visual Basic 5.0 (2nd Edition) / Hardcore Visual Basic 5.0 - Second Edition (1997)(Microsoft Press).iso / Code / Sharestr.cls < prev    next >
Text File  |  1997-06-14  |  3KB  |  93 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "CSharedString"
  6. Attribute VB_GlobalNameSpace = False
  7. Attribute VB_Creatable = True
  8. Attribute VB_PredeclaredId = False
  9. Attribute VB_Exposed = True
  10. Option Explicit
  11.  
  12. Public Enum EErrorSharedString
  13.     eeBaseSharedString = 13200  ' CSharedString
  14. End Enum
  15.  
  16. Private h As Long, p As Long
  17.  
  18. Sub Create(sName As String)
  19.     Dim e As Long
  20.     If sName = sEmpty Then ApiRaise ERROR_BAD_ARGUMENTS
  21.     ' Try to create file mapping of 65535 (only used pages matter)
  22.     h = CreateFileMapping(-1, pNull, PAGE_READWRITE, 0, 65535, sName)
  23.     ' Save "error" value which may not be an error value
  24.     e = Err.LastDllError
  25.     If h = hNull Then ApiRaise e
  26.     
  27.     ' Get pointer to mapping
  28.     p = MapViewOfFile(h, FILE_MAP_WRITE, 0, 0, 0)
  29.     If p = pNull Then
  30.         CloseHandle h   ' Undo what we did
  31.         ApiRaise Err.LastDllError
  32.     End If
  33.     ' Check cached value to see if new value
  34.     If e <> ERROR_ALREADY_EXISTS Then
  35.         ' Set size of new file mapping by copying 0 to first 4 bytes
  36.         CopyMemory ByVal p, 0, 4
  37.     ' Else
  38.         ' Existing file mapping already initialized
  39.     End If
  40. End Sub
  41.  
  42. Private Sub Class_Terminate()
  43.     UnmapViewOfFile p
  44.     CloseHandle h
  45. End Sub
  46.  
  47. ' Default property
  48. Property Get Item() As String
  49. Attribute Item.VB_UserMemId = 0
  50.     If h = hNull Then ErrRaise ERROR_INVALID_DATA
  51.     BugAssert p <> pNull
  52.     ' Copy length out of first 4 bytes of data
  53.     Dim c As Long
  54.     CopyMemory c, ByVal p, 4
  55.     If c Then
  56.         ' Copy the data
  57.         Item = String$(c, 0)
  58.         CopyMemoryToStr Item, ByVal (p + 4), c * 2
  59.     End If
  60. End Property
  61.  
  62. Property Let Item(s As String)
  63.     If h = hNull Then ErrRaise ERROR_INVALID_DATA
  64.     BugAssert p <> pNull
  65.     Dim c As Long
  66.     c = Len(s)
  67.     ' Copy length to first 4 bytes and string to remainder
  68.     CopyMemory ByVal p, c, 4
  69.     CopyMemoryStr ByVal (p + 4), s, c * 2
  70. End Property
  71. '
  72.  
  73. #If fComponent = 0 Then
  74. Private Sub ErrRaise(e As Long)
  75.     Dim sText As String, sSource As String
  76.     If e > 1000 Then
  77.         sSource = App.ExeName & ".SharedString"
  78.         Select Case e
  79.         Case eeBaseSharedString
  80.             BugAssert True
  81.        ' Case ee...
  82.        '     Add additional errors
  83.         End Select
  84.         Err.Raise COMError(e), sSource, sText
  85.     Else
  86.         ' Raise standard Visual Basic error
  87.         sSource = App.ExeName & ".VBError"
  88.         Err.Raise e, sSource
  89.     End If
  90. End Sub
  91. #End If
  92.  
  93.